import { useState, useEffect, useRef } from 'react';
import { message } from 'antd';
import { fetchVersionlist, fetchPostlist } from '@/services/setting/salaryReference';

// 从服务器刷新邮件列表
const useEditionList = ({ setVersion, urlType = 'salary', setApplicationNo }) => {
    const isFirstRun = useRef(true);
    const [loading, setLoading] = useState(false);
    const [resData, setResData] = useState(null);
    const fetchData = async () => {
        setLoading(true);
        const { data, code, msg } = urlType === 'salary' ? await fetchVersionlist() : await fetchPostlist();
        if (code === 10000) {
            setResData(data);
            // 仅第一次运行
            if (isFirstRun.current) {
                isFirstRun.current = false;
                setVersion(data.length > 0 ? data[0].salaryStandardVersion || data[0].postitionSalaryVersion : '');
                if (urlType === 'salary') {
                    setApplicationNo(data.length > 0 ? data[0].applicationNo : '');
                }
                // return;
            }
        } else if (msg && msg.length > 0) {
            message.error(msg);
        }
        setLoading(false);
    };
    useEffect(() => {
        fetchData();
    }, []);
    return [loading, resData];
};

export default useEditionList;
